# coding=utf-8

import subprocess
import time
import datetime
# from .utils.screen_snapshot import get_snapshot
# from utils.screen_snapshot import get_snapshot
import sys
sys.path.append(r'E:\codepiece\junp-4-double-twelve')
from utils.screen_snapshot import get_snapshot

JUMP_POSITION = (536, 884)
RESULT_CLOSE_POSITION = (854, 560)
JUMP_ANIMATION_DURATION_IN_SECONDS = 15
RESULT_ANIMATION_DURATION_IN_SECONDS = 1
SCREEN_CAP_PATH = 'E:\\codepiece\\junp-4-double-twelve'


def connect_phone():
    return True
    result = subprocess.check_output("adb devices")
    print(result)
    return " device " in result


def touch_screen(x, y):
    subprocess.Popen("adb shell input  touchscreen tap {x} {y}".format(x=x, y=y)).wait()


if __name__ == "__main__":
    if not connect_phone():
        print("Connect phone failed")

    start_time = datetime.datetime.now().strftime("%Y-%m-%d_%H_%M_%S")
    get_snapshot("{}\{}-start.png".format(SCREEN_CAP_PATH, start_time))
    for i in range(200):
        print(u"{:8d} 次跳一跳".format(i+1))
        # 按一下起跳
        touch_screen(JUMP_POSITION[0], JUMP_POSITION[1])

        print("等待 {} 起跳动画结束".format(JUMP_ANIMATION_DURATION_IN_SECONDS))
        # 等待起跳动画完结，显示结果
        time.sleep(JUMP_ANIMATION_DURATION_IN_SECONDS)

        print("关闭结果，再次起跳准备")
        # 关闭结果
        touch_screen(RESULT_CLOSE_POSITION[0], RESULT_CLOSE_POSITION[1])

        # 等待起跳画面再次就绪
        print("等待 {} 结果动画结束".format(RESULT_ANIMATION_DURATION_IN_SECONDS))
        time.sleep(RESULT_ANIMATION_DURATION_IN_SECONDS)

    get_snapshot("{}\\{}-end.png".format(SCREEN_CAP_PATH, start_time))
